﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ADO数据库编程
{

 

    class DapperDemo
    {
        public IEnumerable<Person> SelectUser()
        {
            using (var connection = SqlHelper.CreateConn())
            {
                var persons = connection.Query<Person>(" SELECT * FROM Person ORDER BY id asc ");
                return persons;
            }
        }

        public Person GetPersonById(long id)
        {
            using (var connection = SqlHelper.CreateConn())
            {
                var person = connection.QuerySingleOrDefault<Person>("SELECT * FROM Person WHERE Id = @Id", new { Id = id });
                return person;
            }
        }

        public int AddPerson(Person person)
        {
            using (var connection = SqlHelper.CreateConn())
            {
                int v = connection.Execute("INSERT INTO Person (name,age,sex,addr) VALUES (@name, @age,@sex,@addr)", person);
                return v;
            }
        }


        public int UpdatePerson(Person person)
        {
            using (var connection = SqlHelper.CreateConn())
            {
               int v =  connection.Execute("UPDATE Person SET name = @name,age = @age,sex = @sex,addr = @addr WHERE Id = @Id", person);
               return v;
            }
        }


        public int DeletePerson(long id)
        {
            using (var connection = SqlHelper.CreateConn())
            {
                int v = connection.Execute("DELETE FROM Person WHERE Id = @Id", new { Id = id });
                return v;
            }
        }
    }
}
